<?php

namespace App\Models;

class Cookbook extends Base
{
    protected $appends = ['vip_price_yuan', 'price_yuan'];

    protected $fillable = [
        'merchant_id', 'category_id', 'video_url', 'short_video_url', 'long_time', 'difficulty', 'title', 'short_title',
        'is_free', 'is_vip', 'price', 'calorie', 'proteide', 'carbohydrate', 'fat', 'list_food', 'step', 'sort', 'audit_status',
        'is_on', 'comment_num', 'note_num', 'star_num'
    ];

    protected $casts = [
        'step' => 'array',
        'list_food' => 'array'
    ];

    public function merchant() {
        return $this->belongsTo(Merchant::class, 'merchant_id');
    }

    public function category() {
        return $this->belongsTo(Category::class, 'category_id');
    }

    public function setListFoodAttribute($list_food) {
        return $this->attributes['list_food'] = $list_food;
    }

    public function setStepAttribute($step) {
        return $this->attributes['step'] = $step;
    }

    public function getPriceYuanAttribute() {
        return format_yuan($this->price);
    }

    public function getVipPriceYuanAttribute() {
        return format_yuan($this->price * Config::getDiscount());
    }

    public function is_star() {
        return $this->hasOne(Star::class, 'entity_id')->where('type', Star::TYPE_COOKBOOK);
    }

    public function view_history() {
        return $this->hasOne(UserViewHistory::class, 'cookbook_id', 'id');
    }
}
